#include "stdafx.h"
#include "dataProcess.h"
#include "dataProcessDef.h"
#include "datastruct.h"
#include <afx.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

frontManageData* theData = NULL;

bool GetAreaInfo(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	else
	{
		return true;
	}
}

bool SetAreaInfo(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		int nCount = 0;
		int nSize = 0;
		void* pTempBuffer = netDataIn->buffer;
		MAC_READVAR(pTempBuffer,int,nCount,nSize);
		vector<areaInfo> vAreaInfo(nCount);
		for (int i = 0; i < nCount; i++)
		{
			nSize = vAreaInfo[i].load(pTempBuffer);
			pTempBuffer = (char*)pTempBuffer + nSize;
		}
		theData->Lock();
		theData->setAreaInfo(vAreaInfo);
		theData->UnLock();
		return false;
	}
	return true;
}

bool checkUserName(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	return false;
}

bool setUserRight(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		userAccount* nowAccount = (userAccount*)netDataIn->buffer;
		theData->SetNowUser(*nowAccount);
		return false;
	}
	return false;
}

bool getComName(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	return false;
}

bool setComName(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		wchar_t* nowAccount = (wchar_t*)netDataIn->buffer;
		theData->SetCompanyName(nowAccount,netDataIn->bufferLength);
		//theData->SetNowUser(*nowAccount);
		return false;
	}
	return false;
}

bool getComPic(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	return false;
}

bool setComPic(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		CFile imageFile(theData->m_Path + L"comanyPic.jpg",CFile::modeCreate | CFile::modeWrite);
		imageFile.Write(netDataIn->buffer,netDataIn->bufferLength);
		imageFile.Close();
		theData->setPictureName(L"comanyPic.jpg");
		return false;
	}
	return false;
}

bool getEmployeeSInfo(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	return false;
}

bool setEmployeeSInfo(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		employeeShortInfo* pShortInfo = (employeeShortInfo*)netDataIn->buffer;
		theData->setEmployeeShort(*pShortInfo);
		return false;
	}
	return false;
}

bool getBookSetData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	return false;
}

bool setBookSetData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		bookSetData bookData;
		bookData.load(netDataIn->buffer);
		theData->setBookSetData(bookData);
		return false;
	}
	return false;
}

bool getBookData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	return false;
}

bool setBookData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		void* pBuffer = netDataIn->buffer;
		int nSize = 0;
		int nCount = 0;
		MAC_READVAR(pBuffer,int,nCount,nSize);

		vector<bookManagerData> vData(nCount);
		for(int i = 0; i < nCount; i++)
		{
			MAC_READVAR(pBuffer,bookManagerData,vData[i],nSize);
		}
		theData->setBookData(vData);
		return false;
	}
	return false;
}

bool sendNetData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (isSend)
	{
		theData->getSocket()->send(*netDataIn);
		return false;
	}
	return false;
}

bool setFoodParam(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		foodParam* foodName = (foodParam*)netDataIn->buffer;
		theData->setFoodParam(foodName);
		return false;
	}
	return false;
}

bool setOrderOption(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		orderOption* pOrderOption = (orderOption*)netDataIn->buffer;
		theData->setOrderOption(*pOrderOption);
		return false;
	}
	return false;
}
bool setOrder(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		//theData->setSerial(pOrderOption->areaName,pOrderOption->deskName,pOrderOption->nSerial);
		Order newOrder;
		newOrder.load(netDataIn->buffer);
		theData->setOrder(newOrder.areaName,newOrder.deskName,newOrder);
		return false;
	}
	return false;
}

bool setAllDiscountPlan(netData* netDataIn,netData* netDataOut,bool isSend)
{
	ASSERT(theData);
	if (!isSend)
	{
		void* tmpBuffer = netDataIn->buffer;
		int nSize = 0;
		int nCount = 0;
		MAC_READVAR(tmpBuffer,int,nCount,nSize);
		vector<discountPlan> vDiscount(nCount);
		for (int i = 0; i < nCount; i++)
		{
			nSize = vDiscount[i].load(tmpBuffer);
			tmpBuffer = (char*)tmpBuffer + nSize;
		}
		theData->setAllDiscountPlan(vDiscount);
		return false;
	}
	return false;
}

bool delCombine(netData* netDataIn,netData* netDataOut,bool isSend)
{
	getDeskOrder* desk = (getDeskOrder* )netDataIn->buffer;
	theData->delCombine(desk->areaName,desk->deskName);
	return false;
}

bool processMain(void* paramIn,void* paramOut,bool isSend)
{
	netData* netDataIn = (netData*)paramIn;
	netData* netDataOut = (netData*)paramOut;
	switch (netDataIn->type)
	{
	case dataProcessTypeGetAeraInfo:
		return GetAreaInfo(netDataIn,netDataOut,isSend);
	case dataProcessTypeSetAeraInfo:
		return SetAreaInfo(netDataIn,netDataOut,isSend);
	case checkUserNameIsRight:
		return checkUserName(netDataIn,netDataOut,isSend);
	case setUserNameRight:
		return setUserRight(netDataIn,netDataOut,isSend);
	case getCompanyName:
		return getComName(netDataIn,netDataOut,isSend);
	case setCompanyName:
		return setComName(netDataIn,netDataOut,isSend);
	case getCompanyPicture:
		return getComPic(netDataIn,netDataOut,isSend);
	case setCompanyPicture:
		return setComPic(netDataIn,netDataOut,isSend);
	case getEmployeeShortInfo:
		return getEmployeeSInfo(netDataIn,netDataOut,isSend);
	case setEmployeeShortInfo:
		return setEmployeeSInfo(netDataIn,netDataOut,isSend);
	case dataProcessGetBookSetData:
		return getBookSetData(netDataIn,netDataOut,isSend);
	case dataProcessSetBookSetData:
		return setBookSetData(netDataIn,netDataOut,isSend);
	case dataProcessGetBookData:
		return getBookData(netDataIn,netDataOut,isSend);
	case dataProcessSetBookData:
		return setBookData(netDataIn,netDataOut,isSend);
	case dataProcessAddBookData:
		return sendNetData(netDataIn,netDataOut,isSend);
	case dataProcessDelBookData:
		return sendNetData(netDataIn,netDataOut,isSend);
	case dataProcessGetFoodParam:
		return sendNetData(netDataIn,netDataOut,isSend);
	case dataProcessSetFoodParam:
		return setFoodParam(netDataIn,netDataOut,isSend);
	case dataProcessGetOrderOption:
		return sendNetData(netDataIn,netDataOut,isSend);
	case dataProcessSetOrderOption:
		return setOrderOption(netDataIn,netDataOut,isSend);
	case dataProcessUpdateOrder:
		setOrder(netDataIn,netDataOut,false);
		return sendNetData(netDataIn,netDataOut,isSend);
	case dataProcessGetOrder:
	case dataProcessGetAllDiscountPlan:
		return sendNetData(netDataIn,netDataOut,isSend);
	case dataProcessSetOrder:
		return setOrder(netDataIn,netDataOut,isSend);
	case dataProcessSetAllDiscountPlan:
		return setAllDiscountPlan(netDataIn,netDataOut,isSend);
	case dataProcessCheckOut:
		delCombine(netDataIn,netDataOut,isSend);
		return sendNetData(netDataIn,netDataOut,isSend);
	case dataProcessSetState:
		return sendNetData(netDataIn,netDataOut,isSend);

	}
	return false;
}